package org.sjsu.freedoor.replication;

import java.util.ArrayList;
import java.util.Collection;
import org.sjsu.freedoor.model.*;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.Client;


public class ReplicationThread extends Thread{

	public static final int DELAY=30000;
	
	public void run(){
		System.out.println("[STARTUP]  FreeDoor replication thread running every " + DELAY + " seconds...");
		
		while(true){
			System.out.println("pulling data from known communities...");
			pullData();
			try {
				ReplicationThread.sleep(DELAY);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	

	private void pullData() {
		
		
		ArrayList<String> url = new ArrayList<String>(); //list of known communities URL
		
		
		//get the list of all communities first
		Collection<Community> coms = Freedoor.getInstance().getCommunities().toCollection();
		
		
		
		//for(int i=0; i< coms.length; i++){
		for(Community com : coms){
			try {
				String currentURL=com.getURL();
				if(!url.contains(currentURL)){
					url.add(currentURL);
					System.out.println(currentURL);
				}
				
			} catch (Exception e) {
				System.out.println("Incorrectly formatted community URL:" + com.getURL());
				e.printStackTrace();
			}
		}
		
		
		//update communities
		for(int i=0; i<url.size();i++){
			//get communities from other all community we know
			try{
			
				Client client = Client.create();
				WebResource webResource = client.resource(url.get(i) + "/communities");
				String s = webResource.get(String.class);
				
				//consume the JSON reply from other communities
				Freedoor fd = Freedoor.getInstance();
				fd.postCommunities(s);
				
			}
			catch(Exception e){
				System.out.println("Error parsing community data from " + url.get(i));
				e.printStackTrace();
			}
		}
		
		//get the list of known communities again
		coms = Freedoor.getInstance().getCommunities().toCollection();		
		//for(int i=0; i< coms.length; i++){
		for(Community com : coms){
			try {
				String currentURL=com.getURL();
				if(!url.contains(currentURL)){
					url.add(currentURL);
					System.out.println(currentURL);
				}
				
			} catch (Exception e) {
				System.out.println("Incorrectly formatted community URL:" + com.getURL());
				e.printStackTrace();
			}
		}		
		
		//update services
		for(int i=0; i<url.size();i++){
			//get communities from other all community we know
			try{
				//iterate through the pages (assume no more pages if last page is less than 10)
				boolean stop = false;
				int page = 1;
				while(!stop){
					
					Client client = Client.create();
					WebResource webResource = client.resource(url.get(i) + "/services?page=" + page);
					String s = webResource.get(String.class);
					
					//consume the JSON reply from other communities
					Freedoor fd = Freedoor.getInstance();
					
					//post to services
					ServiceList serv = new ServiceList();
					serv.loadServices(s);
					serv.removeForeignServices(url.get(i));
					
					fd.insertOrUpdateServices(serv);
					
					//check if there are more pages
					if(serv.size() < 10)
						stop = true;
					else
						page++;
				}				
			}
			catch(Exception e){
				System.out.println("Error parsing services data from " + url.get(i));
				e.printStackTrace();
			}
		}
		
		
		//update goods
		for(int i=0; i<url.size();i++){
			//get communities from other all community we know
			try{
				//iterate through the pages (assume no more pages if last page is less than 10)
				boolean stop = false;
				int page = 1;
				while(!stop){
					
					Client client = Client.create();
					WebResource webResource = client.resource(url.get(i) + "/goods?page=" + page);
					String s = webResource.get(String.class);
					
					//consume the JSON reply from other communities
					Freedoor fd = Freedoor.getInstance();
					
					//post to services
					GoodList serv = new GoodList();
					serv.loadGoods(s);
					serv.removeForeignGoods(url.get(i));
					
					fd.insertOrUpdateGoods(serv);
					
					//check if there are more pages
					if(serv.size() < 10)
						stop = true;
					else
						page++;
				}				
			}
			catch(Exception e){
				System.out.println("Error parsing goods data from " + url.get(i));
				e.printStackTrace();
			}
		}		
	}
	
}
